﻿using System;
using MedicWeb.MedicService;
using Microsoft.Reporting.WebForms;

namespace MedicWeb.forms.Reports.Invoice
{
    public partial class ReportInvoice : SessionPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = Request.QueryString["id"];
            //string date = Request.QueryString["date"];
            //string number = Request.QueryString["number"];
            int id = s == null ? 0 : int.Parse(s);

            Response.ContentType = "application/pdf";

            //ReportViewer1.ProcessingMode = ProcessingMode.Local;
            //ReportViewer1.LocalReport.ReportPath = @"forms\Reports\Invoice\ReportInvoice.rdlc";

            
            
            Doctor doctor = null;
            if (!IsPostBack)
            {
                var ms = new MainService();
                if (id > 0)
                {
                    doctor = ms.LoadDoctor(GetSession(), id);
                }
                if (doctor == null)
                {
                    throw new Exception("Аттестуемый не сохранён");
                }
              
                //PrintDoctor printDoctor = ms.GetPrintDoctor(GetSession(), doctor.Id);
                PrintInvoice printInvoice = ms.GetPrintInvoice(GetSession(), doctor);
                //if (date != null) {
                //    printInvoice.Text = printInvoice.Text.Replace("$date", date);
                //}
                //if (number != null) {
                //    printInvoice.Text = printInvoice.Text.Replace("$number", number);
                //}
                //STUB
                //printOrder.Text = printOrder.Text.Replace("$date", DateTime.Now.ToShortDateString())
                //                                 .Replace("$number", "testNumber");
                //ENDSTUB
                //var sourceDoctors = new ReportDataSource("DataSetDoctors", new[] {printDoctor});
                //ReportViewer1.LocalReport.DataSources.Add(sourceDoctors);
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("Accepter", printInvoice.Accepter));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("BIK", printInvoice.BIK));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("Bank", printInvoice.Bank));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("INN", printInvoice.INN));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("KPP", printInvoice.KPP));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("SP", printInvoice.SP));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("Ksch", printInvoice.Ksch));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("Ls", printInvoice.Ls));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("Summ", printInvoice.Summ));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("Vid", printInvoice.Vid));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("FIO", doctor.Surname + " " + doctor.Name + " " + doctor.Patronymic));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("Address", doctor.Address));
                //ReportViewer1.LocalReport.Refresh();

                //ReportViewer1.LocalReport.ReportEmbeddedResource = MapPath("")
            }

            byte[] render = ReportViewer1.LocalReport.Render("PDF");
            Response.BinaryWrite(render);
            Response.End();

            
        }
    }
}